package helper
{
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Bounce;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import screen.SacombankScreen;
	import screen.component.Resource;
	import screen.component.UI;
	
	
	public class Reward extends UI
	{
		private var _reward:Object;
		private var _disp_mc:MovieClip;
		private var _target_mc:MovieClip;
		public static var instance:Reward;
		public function Reward():void
		{	
			if(!instance){
				instance = this;
			}
		}
		public static function getInstance():Reward{
			if(!instance){
				instance = new Reward()
			}
			return instance;
		}
		public function showOn(disp:MovieClip,target:MovieClip, thisReward:Object, loadingBar:Boolean = false, callback:Function = null):void {

			_reward =  thisReward;
			_disp_mc = disp;
			_target_mc 	= target;
			if (loadingBar) {
				//LoadingHelper.addCallback(thisTargetMC, throughReward, callback);
			} else {
				throughReward(disp);
			}
			
		}
		
		private function throughReward(disp:DisplayObject) : void {
			var corX:Number = disp.parent.localToGlobal(new Point(disp.x, disp.y)).x + disp.width/2;
			var corY:Number = disp.parent.localToGlobal(new Point(disp.x, disp.y)).y;
			var type:String =_reward.star>0?"star":"gold";
			var oneReward:MovieClip = ValueObject.S1_RESOURCE.getExportedAsset(type) as MovieClip;
			oneReward.x = corX;
			oneReward.y = corY;
			oneReward.scaleX = oneReward.scaleY = 0.2;
			//disp.parent.addChild(oneReward);
			Sacombank.getInstance().addChild(oneReward);
			TweenMax.to(oneReward, 1, {bezierThrough:[	{x:oneReward.x+randomRange(30, 20), y:oneReward.y - randomRange(20, 15)}, 
				{x:oneReward.x+randomRange(40, 30), y:oneReward.y + randomRange(60, 50)}], 
				orientToBezier:true, scaleX:0.5, scaleY:0.5, ease:Bounce.easeOut});
			oneReward.addEventListener(MouseEvent.ROLL_OVER, flyAwayEXP);
			oneReward.timeOut = setTimeout(flyAwayEXP, 5000, null, oneReward);
			
		}
		
		private  function randomRange(max:Number, min:Number=0) : Number
		{
			return Math.random() * (max - min) + min;
		}
		
		private function flyAwayEXP(e:MouseEvent, mc:MovieClip = null) : void {
			var targetMC:MovieClip = (e != null) ? MovieClip(e.currentTarget) : mc;
			targetMC.removeEventListener(MouseEvent.ROLL_OVER, flyAwayEXP);
			clearTimeout(targetMC.timeOut);
			var textFly:MovieClip 	= ValueObject.S1_RESOURCE.getExportedAsset("TextFlyMC") as MovieClip;
			textFly.x = targetMC.x;
			textFly.y = targetMC.y;
			if(_reward.star>0){
				Resource.getInstance().addStar(_reward.star);
			}else{
				Resource.getInstance().addGold(_reward.gold);
			}
			textFly.mc.tf.text = "+"+String(_reward.star>0?_reward.star:_reward.gold);
			Sacombank.getInstance().addChild(textFly);
			textFly.gotoAndPlay(1);
			var p:Point;
			
			if(_target_mc.parent !=SacombankScreen.getInstance()){
				p = _target_mc.localToGlobal(new Point(_target_mc.x,_target_mc.y));
			}else{
				p = new Point(_target_mc.x,_target_mc.y);
				_target_mc.pigMc.gotoAndStop(2);
			}
			
			TweenLite.to(targetMC, 1.5, {x:p.x, y:p.y,scaleX:0.5,scaleY:0.5,alpha:0.7, onCompleteParams:[targetMC], onComplete:function(mctarget:MovieClip):void {
				Sacombank.getInstance().removeChild(mctarget);
			}});	
		}
		
		
	}	
}